//域名 端口号
//var baseUrl = 'https://wzxlovemzy.com:6001/user'

import {
	baseUrl
} from "./request.js"


export function uploadFile(filePath) {
	return new Promise((resolve, rejected) => {

		//判断当前设备
		var reqFormData = {}
		if (uni.getSystemInfoSync().platform == 'android') {
			//console.log("android机")
			reqFormData = {
				file: filePath,
				userId: uni.getStorageSync('userId')
			}
		} else {
			//console.log("ios机")
			reqFormData = {
				userId: uni.getStorageSync('userId')
			}
		}

		uni.showLoading({
			title: '加载中...'
		});
		wx.uploadFile({
			url: baseUrl + '/file/uploadFile',
			name: 'file',
			filePath: filePath,
			header: {
				'token': uni.getStorageSync('token')
				// "Content-Type":""
			},
			formData: reqFormData,
			success: (res) => {
				if (res.statusCode !== 200) {
					uni.showToast({
						icon: 'none',
						duration: 3000,
						title: `网络异常`
					});
				}

				resolve(JSON.parse(res.data)) //如果请求成功，调用resolve返回数据
				uni.hideLoading()

			},
			fail: (err) => { // err 是返回来的错误信息
				uni.showToast({
					icon: 'none',
					duration: 3000,
					title: `请求失败`

				});
				rejected(err) // 调用reject方法把错误消息返回出去
				uni.hideLoading()
			}
		})

	})

}